/* -----------------
* brief
* 1. this is code gen by tools
* 2. mvc code are driver by the model defined with the same filename,
	 for example:
	 there is a Xmodel struct define in xmodel.go file, after the script run (lego model-mvc module_name xmodel)
     it will auto create the code under the module's path
	 m --> xmodel.go
	 v --> view
	 c --> xmodel_ctrl
* 3. you should modify this code if it doesn't satisfy your purpose
*/

package controller

import (
	"encoding/json"
	wxcard "github.com/chanxuehong/wechat/mp/card"
	wxacitveuserform "github.com/chanxuehong/wechat/mp/card/membercard/activateuserform"
	wxcardqr "github.com/chanxuehong/wechat/mp/card/qrcode"
	testwhitelist "github.com/chanxuehong/wechat/mp/card/testwhitelist"
	"github.com/mjiulee/lego"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
	. "saas_sys_base/modules/wechat/service"
)

type WechatCardApiCtrl struct {
	wxCardService WxCardService
}

func init() {
	vc := new(WechatCardApiCtrl)
	_ctrlList = append(_ctrlList, vc)
}

func (vc *WechatCardApiCtrl) CtrlInfo() string {
	return "WechatCardApiCtrl"
}

func (vc *WechatCardApiCtrl) RoutInit() {
	//lego.APIPOSTv2("/api/wechat/card/create_card2", vc.CreateCard2, false)
	lego.APIPOST("/api/mod/wechat/card/create", vc.CreateCard, true)
	lego.APIPOST("/api/mod/wechat/card/card/batchget", vc.BatchGet, true)
	lego.APIPOST("/api/mod/wechat/card/card/get", vc.GetCard, true)
	lego.APIPOST("/api/mod/wechat/card/testwhitelist", vc.TestWhileList, true)
	lego.APIPOST("/api/mod/wechat/card/membercard/activateuserform/set", vc.SetActivateUserForm, true)
}

func (vc *WechatCardApiCtrl) CreateCard2_deleted(ctx *fasthttp.RequestCtx, req *wxcard.CardMsg) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Resp struct {
		CardId string `json:"card_id"`
	}

	var resp Resp

	for {

		/*cardid, err := vc.WechatService.CreateCard(&req.Card)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.CardId = cardid*/
		response["data"] = req
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) CreateCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	type Request struct {
		wxcard.CardMsg
	}
	type Resp struct {
		CardId string `json:"card_id,omitempty"`
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		appwho := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request.CardMsg)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		cardid, err := vc.wxCardService.CreateCardByWho(*appwho, appwho.TenantId, &request.Card)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.CardId = cardid
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) BatchGet(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"
	type Request struct {
		wxcard.BatchGetQuery
	}
	type Resp struct {
		wxcard.BatchGetResult
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		// TODO 需要修改接口参数
		result, err := vc.wxCardService.BatchGetByWho(*who, &request.BatchGetQuery)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.BatchGetResult = *result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) GetCard(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		CardId string `json:"card_id"`
	}

	type Resp struct {
		wxcard.Card
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		result, err := vc.wxCardService.GetCardByWho(*who, request.CardId)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.Card = *result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) CreateCardQrCode(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		wxcardqr.CreateQrCodeReqPara
	}

	type Resp struct {
		*wxcardqr.QrcodeInfo
		ShowQrUrl string `json:"show_qr_url"`
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		result, err := vc.wxCardService.CreateQrCodeByWho(*who, &request.CreateQrCodeReqPara)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		resp.QrcodeInfo = result

		resp.ShowQrUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + result.Ticket
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) TestWhileList(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		testwhitelist.SetParameters
	}

	type Resp struct {
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		err = vc.wxCardService.TestWhileListByWho(*who, &request.SetParameters)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//resp. = result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}

func (vc *WechatCardApiCtrl) SetActivateUserForm(ctx *fasthttp.RequestCtx) {
	ctxExt := lego.RequestCtxExtent{ctx}
	response := make(map[string]interface{})
	code := 0
	msg := "success"

	type Request struct {
		wxacitveuserform.ActiveuserFormPara
	}

	type Resp struct {
	}

	var request Request
	var resp Resp

	for {
		sessionData := common.GetSessionData(&ctxExt)
		who := sessionData.AppTokenWho

		requestByte := ctxExt.PostBody()
		err := json.Unmarshal(requestByte, &request)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}
		// TODO 需要修改接口参数
		err = vc.wxCardService.SetActivateUserForm(*who, &request.ActiveuserFormPara)
		if err != nil {
			code = -1
			msg = err.Error()
			break
		}

		//resp. = result
		//response["data"] = resp
		break
	}

	response["code"] = code
	response["msg"] = msg
	response["data"] = resp
	ctxExt.JSON(200, response)

}
